package com.collect.senegal.zhiUtils.zhiApkDownload;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;

import androidx.core.content.FileProvider;

import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import static com.collect.senegal.zhiUtils.PackageUtil.getPackageName;

public class ApkDownloadHelper {
    private static SharedPreferences sharedPreferences;
    private static final String SP_NAME = "chaos_downloader_progress_file_name";

    public static SharedPreferences getSharedPreferences(Context context) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        }
        return sharedPreferences;
    }

    public static void putCurrLengthValue(Context context, String key, int value) {
        getSharedPreferences(context)
                .edit()
                .putInt(key, value)
                .apply();
    }

    public static int getCurrLengthValue(Context context, String key) {
        return getSharedPreferences(context).getInt(key, 0);
    }

    public static void removeCurrLengthValue(Context context, String key) {
         getSharedPreferences(context).edit().remove(key).commit();
    }

    public static boolean installApk(Context context, String apkPath) {
        try {
            context.startActivity(getInstallApkIntent(context, apkPath));
            return true;
        }
        catch (Exception e){

        }
        return false;

    }

    public static Intent getInstallApkIntent(Context context, String apkPath) {
        try {
            File apkFile = new File(apkPath);
            if (!apkFile.exists()) {
                throw new FileNotFoundException("Apk file does not exist!");
            }

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Uri apkUri;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                apkUri = FileProvider.getUriForFile(context, getPackageName(context) + ".tool.file_provider", apkFile);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            } else {
                apkUri = Uri.fromFile(apkFile);
            }
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
            return intent;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void closeQuietly(final Closeable... closeables) {
        if (closeables == null || closeables.length == 0) {
            return;
        }

        for (Closeable closeable : closeables) {
            if (closeable == null) {
                continue;
            }

            try {
                closeable.close();
            } catch (IOException ignored) {
            }
        }
    }
}
